<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/2/24
 * Time: 15:25
 */

namespace SDXApi\Service;


use SDXApi\Abstracts\TemplateAbstract;
use SDXApi\Model\HomeTemplateStencilGoodsModel;
use SDXApi\Repositories\GoodsRepository;

/**
 * Class HomeTemplateGoodsService
 * @package SDXApi\Service
 */
class HomeTemplateGoodsService extends TemplateAbstract
{
	/**
	 * @var HomeTemplateStencilGoodsModel
	 */
	protected $templateStencilGoodsModel;

	/**
	 * @var GoodsRepository
	 */
	protected $goodsRepository;

	/**
	 * HomeTemplateGoodsService constructor.
	 */
	public function __construct()
	{
		$this->templateStencilGoodsModel = new HomeTemplateStencilGoodsModel();
		$this->goodsRepository = new GoodsRepository();
	}

	/**
	 * Created by Jerry
	 * @param array $stencil
	 * @param int $userId
	 * @param int $wid
	 * @return array|bool
	 */
	public function handle($stencil = array(), $userId = 0, $wid = 8)
	{
		return $this->stencilGoods($stencil, $userId, $wid);
	}

	/**
	 * 处理商品模块数据
	 * Created by Jerry
	 * @param array $stencil
	 * @param int $userId
	 * @param int $wid
	 * @return array|bool
	 */
	protected function stencilGoods($stencil = array(), $userId = 0, $wid = 8)
	{

//		商品数据处理
		$this->goodsRepository->userId = $userId;
		$this->goodsRepository->wid = $wid;

		$goods = $this->getGoodsCacheData($stencil, $userId, $wid);

		if (empty($goods)) {
			$goods = array();
		}

		if (count($goods) > 1) {
			$goods = self::checkGoodsDataSort($goods);
		}

		if ($stencil['goods_limit'] > 0) {
		    $data = array_chunk($goods, $stencil['goods_limit']);
		    $goods = $data[0];
        }

		$goods = array_merge($goods);
		$stencil['goods_list'] = $goods;

		return $stencil;
	}

    /**
     * 排序
     * Created by Jerry
     * @param array $data
     * @return array
     */
    static protected function checkGoodsDataSort($data = array())
    {

        $sort = array();
        foreach ($data as $key => $value) {

//            if (self::checkPromoteGoods($value) && $value['goods_number'] > 0) {
//                $sort[] = 999999;
//                continue;
//            }

            if (empty($value['goods_number'])) {
                $sort[] = $value['goods_number'];
                continue;
            }

            if (! empty($value['sort'])) {
                $sort[] = $value['sort'];
                continue;
            } else {
                $sort[] = 1;
            }
        }

        array_multisort($sort, SORT_NUMERIC, SORT_DESC, $data);

        return $data;
    }

    /**
     * @author Jerry
     * @param array $goods
     * @return bool
     */
    protected static function checkPromoteGoods($goods = array())
    {

        if ($goods['promote_price'] > 0) {
            return true;
        }

        return false;
    }

	/**
	 * 获取商品缓存数据
	 * Created by Jerry
	 * @param array $stencil
	 * @param int $userId
	 * @param int $wid
	 * @return bool|array
	 */
	protected function getGoodsCacheData($stencil = array(), $userId = 0, $wid = 8)
	{
		$userRank = $this->getUserRank($userId);
		$cacheName = 'home_template_goods_id_' . $stencil['id'] . '_rank_' . $userRank . '_wid_' . $wid;
		$goodsFormatData = $this->getCache($cacheName);

		$this->goodsRepository->userId = $userId;
		$this->goodsRepository->wid = $wid;

		if (empty($goodsFormatData)) {
			$goodsData = $this->findTemplateStencilGoodsByStencilId($stencil['id'], $wid);

			if (empty($goodsData)) {
				return false;
			}

			$goodsFormatData = $this->goodsRepository->formatGoodsTransform($goodsData);

			if (! empty($goodsFormatData)) {
				$this->cacheData($cacheName, $goodsFormatData, mt_rand(3600, 4200));
			}
		}

		return $this->goodsRepository->checkGoodsPractical($goodsFormatData);
	}

	/**
	 * Created by Jerry
	 * @param $stencilId
	 * @param int $wid
	 * @return bool
	 */
	protected function findTemplateStencilGoodsByStencilId($stencilId, $wid = 8)
	{
		return $this->templateStencilGoodsModel->findHomeTemplateGoodsByTemplateId($stencilId, $wid);
	}

	/**
	 * Created by Jerry
	 * @param $name
	 * @param $value
	 */
	public function __set($name, $value)
	{
		$this->$name = $value;
	}

	/**
	 * Created by Jerry
	 * @param $data
	 * @param string $sortKey
	 * @return mixed
	 */
	protected static function checkDataSort($data, $sortKey = 'sort')
	{
		$sort = array();
		foreach ($data as $key => $value) {
			$sort[] = $value[$sortKey];
		}

		array_multisort($sort, SORT_DESC, $data);

		return $data;
	}
}